package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsMainBusiness;
import com.ruoyi.system.domain.news.dto.NewsMainBusinessDto;
import com.ruoyi.system.domain.news.query.NewsMainBusinessQuery;
import com.ruoyi.system.domain.news.vo.NewsMainBusinessVo;
import com.ruoyi.system.service.news.NewsMainBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsMainBusiness")
@Api(value = "newsMainBusinessController", tags  = "主要业务")
public class NewsMainBusinessController extends BaseController {

	@Resource
	private NewsMainBusinessService newsMainBusinessService;

    @ApiOperation(value = "新增/修改主要业务信息", notes = "新增/修改主要业务信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsMainBusinessDto newsMainBusinessDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsMainBusiness newsMainBusiness = new NewsMainBusiness();
		BeanUtils.copyProperties(newsMainBusinessDto,newsMainBusiness);
		if (newsMainBusiness.getId()==null) {
			newsMainBusiness.setCreateBy(loginUser.getUsername());
			newsMainBusiness.setDeptId(loginUser.getDeptId());
			newsMainBusiness.setCreateTime(new Date());
			newsMainBusiness.setUpdateBy(loginUser.getUsername());
			newsMainBusiness.setUpdateTime(new Date());
		} else {
			newsMainBusiness.setUpdateBy(loginUser.getUsername());
			newsMainBusiness.setUpdateTime(new Date());
		}
		boolean result = newsMainBusinessService.saveOrUpdate(newsMainBusiness);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除主要业务信息", notes = "删除主要业务信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsMainBusinessService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个主要业务信息", notes = "获取单个主要业务信息")
	@GetMapping("/getById")
	public R<NewsMainBusinessVo> getById(@RequestParam(value = "id") Long id) {
		NewsMainBusinessQuery newsMainBusinessQuery = new NewsMainBusinessQuery();
		newsMainBusinessQuery.setId(id);
		List<NewsMainBusinessVo> newsMainBusinessList = newsMainBusinessService.getNewsMainBusinessList(newsMainBusinessQuery);
		if (newsMainBusinessList!=null&&newsMainBusinessList.size()>0) {
			return R.ok(newsMainBusinessList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取主要业务信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsMainBusinessVo>> list(@RequestBody NewsMainBusinessQuery newsMainBusinessQuery) {
		List<NewsMainBusinessVo> newsMainBusinessList = newsMainBusinessService.getNewsMainBusinessList(newsMainBusinessQuery);
		return R.ok(new PageInfo<>(newsMainBusinessList));
	}
}
